import com.troyworks.particleengine.Particle;
import com.troyworks.framework.ui.layout.Bounds2;
import flash.geom.Rectangle;
import com.troyworks.framework.ui.DraggableClip;
import com.troyworks.particleengine.PhysicsParticle;
import com.troyworks.particleengine.ParticleSim;
/**
 * @author Troy Gardner
 */
class com.troyworks.particleengine.Ch08_15 extends ParticleSim {



	public function Ch08_15() {
		super();
		trace("Ch08_15");	
	}
	

	function onEnterFrame() : Void {
		clear();
		//////////////////// CALCULATE UBIQUITOUS FORCES ////////////////////
		
		for (var i : Number = 0; i < particles.length; i++) {
			var pA : PhysicsParticle = PhysicsParticle(particles[i]);
			var ball : DraggableClip = DraggableClip(pA.view);
			if(gravity > 0 && !ball.isBeingDragged){
				applyGravity(pA);
			}
			if(ball.isBeingDragged){
				trace("dragging " + ball._name);
				drag(pA);
			}
		}
		if(true){
		///////////////////  CALCULATE INTERRELATIONSHIP FORCES /////////
		for (var i : Number = 0; i < particles.length -1; i++) {
			var pA : PhysicsParticle = PhysicsParticle(particles[i]);
			var ballA : DraggableClip = DraggableClip(pA.view);
				for (var j : Number = 0; j < particles.length; j++) {
					var pB : PhysicsParticle = PhysicsParticle(particles[j]);
					var ballB : DraggableClip = DraggableClip(pB.view);
					
					if(!ballA.isBeingDragged){
					//	springTo(pA, pB);
					}
					if(!ballB.isBeingDragged){
					//	springTo(pB, pA);
					}
						//////////// draw the line ///////////
					lineStyle(1, 0, 100);
				//	var b1:Bounds2 = new Bounds(
					moveTo(pA.x,pA.y);
					lineTo(pB.x,pB.y);
					
				}
		}
		}
		
		///////////////// INTEGRATE AND UPDATE UI ////////////////////
		for (var i : Number = 0; i < particles.length; i++) {
			var pA : PhysicsParticle = PhysicsParticle(particles[i]);
			var ball : DraggableClip = DraggableClip(pA.view);
			//if(!ball.isBeingDragged){
				pA.integrateUpdate();
			//	pA.updateView();
			checkBounds(pA);
			//}
		}		
	}
}